using System;
using System.Collections.Generic;
using System.Text;
using Emlyn.Common.Data.SqlServer.SqlDBAccess;
using Emlyn.Common.Data.DBUpdater;
using Emlyn.Common.Data.Configuration;

namespace Emlyn.Website.GeekAngel.DBUpdateRules
{
    public class DUR_AddArticlesTable: IDBUpdaterRule
    {
        #region IDBUpdaterRule Members

        public int UpgradeFromVersion()
        {
            return 10;
        }

        public int UpgradeToVersion()
        {
            return 20;
        }

        public void PerformUpgrade()
        {
            string lCreateArticlesTable =
                "CREATE TABLE [dbo].[tblGAArticle] ( " +
                "   [id] [uniqueidentifier] NOT NULL , " +
                "	[ArticleName] [char] (20) COLLATE Latin1_General_CI_AS NOT NULL , " +
                "	[ArticleBody] [text] COLLATE Latin1_General_CI_AS NOT NULL " +
                ") ";

            string lAddArticlesPK =
                "ALTER TABLE [dbo].[tblGAArticle] WITH NOCHECK ADD " +
                "CONSTRAINT [PK_tblGAArticle] PRIMARY KEY  CLUSTERED " +
                "	( " + 
                "		[id] " + 
                "	)  ";

            string lAddGuidGeneration =
                "ALTER TABLE [dbo].[tblGAArticle] ADD " +
                "	CONSTRAINT [DF_tblGAArticle_id] DEFAULT (newid()) FOR [id] ";

            string lAddArticleNameUniqueIndex =
                "CREATE  UNIQUE  INDEX [ixGAArticle_ArticleName] ON [dbo].[tblGAArticle]([ArticleName]) ";

            SimpleSQL.Execute(Config.GetConnectionString(), lCreateArticlesTable);
            SimpleSQL.Execute(Config.GetConnectionString(), lAddArticlesPK);
            SimpleSQL.Execute(Config.GetConnectionString(), lAddGuidGeneration);
            SimpleSQL.Execute(Config.GetConnectionString(), lAddArticleNameUniqueIndex);
        }

        #endregion
    }
}
